package com.express.mybatis;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageSerializable;

import java.util.Collection;
import java.util.List;
public class PageResult<T>extends PageSerializable<T> {
	private String message;
	private String status;
	private int pageNum;
	private int pageSize;
	@JsonIgnore
	private int size;
	@JsonIgnore
	private int startRow;
	@JsonIgnore
	private int endRow;
	private int pages;
	@JsonIgnore
	private int prePage;
	@JsonIgnore
	private int nextPage;
	@JsonIgnore
	private boolean isFirstPage;
	@JsonIgnore
	private boolean isLastPage;
	@JsonIgnore
	private boolean hasPreviousPage;
	@JsonIgnore
	private boolean hasNextPage;
	@JsonIgnore
	private int navigatePages;
	@JsonIgnore
	private int[] navigatepageNums;
	@JsonIgnore
	private int navigateFirstPage;
	@JsonIgnore
	private int navigateLastPage;

	public PageResult(List<T> list, String message) {
		this(list, message, "200");
	}

	public PageResult(List<T> list, String message, String status) {
		this(list, message, "200", 8);
	}

	public void error(String message, String status) {
		this.message = message;
		this.status = status;
	}

	public PageResult() {
		this.isFirstPage = false;
		this.isLastPage = false;
		this.hasPreviousPage = false;
		this.hasNextPage = false;

		this.status = "200";
	}

	public PageResult(List<T> list) {
		this(list, "");
	}

	public PageResult(List<T> list, String message, String status, int navigatePages) {
		super(list);

		this.message = message;
		this.status = status;

		this.isFirstPage = false;
		this.isLastPage = false;
		this.hasPreviousPage = false;
		this.hasNextPage = false;
		if ((list instanceof Page)) {
			Page page = (Page) list;
			this.pageNum = page.getPageNum();
			this.pageSize = page.getPageSize();
			this.pages = page.getPages();
			this.size = page.size();
			if (this.size == 0) {
				this.startRow = 0;
				this.endRow = 0;
			} else {
				this.startRow = (int) (page.getStartRow() + 1);
				this.endRow = (this.startRow - 1 + this.size);
			}
		} else if ((list instanceof Collection)) {
			this.pageNum = 1;
			this.pageSize = list.size();
			this.pages = (this.pageSize > 0 ? 1 : 0);
			this.size = list.size();
			this.startRow = 0;
			this.endRow = (list.size() > 0 ? list.size() - 1 : 0);
		}
		if ((list instanceof Collection)) {
			this.navigatePages = navigatePages;
			calcNavigatepageNums();
			calcPage();
			judgePageBoudary();
		}
	}

	public static <T> PageResult<T> of(List<T> list) {
		return new PageResult(list);
	}

	private void calcNavigatepageNums() {
		if (this.pages <= this.navigatePages) {
			this.navigatepageNums = new int[this.pages];
			for (int i = 0; i < this.pages; i++) {
				this.navigatepageNums[i] = (i + 1);
			}
		}
		this.navigatepageNums = new int[this.navigatePages];
		int i = this.pageNum - this.navigatePages / 2;
		int endNum = this.pageNum + this.navigatePages / 2;
		if (i < 1) {
			i = 1;
			for (i = 0; i < this.navigatePages; i++) {
				this.navigatepageNums[i] = (i++);
			}
		}
		if (endNum > this.pages) {
			endNum = this.pages;
			for (i = this.navigatePages - 1; i >= 0; i--) {
				this.navigatepageNums[i] = (endNum--);
			}
		}
		for (i = 0; i < this.navigatePages; i++) {
			this.navigatepageNums[i] = (i++);
		}
	}

	private void calcPage() {
		if ((this.navigatepageNums != null) && (this.navigatepageNums.length > 0)) {
			this.navigateFirstPage = this.navigatepageNums[0];
			this.navigateLastPage = this.navigatepageNums[(this.navigatepageNums.length - 1)];
			if (this.pageNum > 1) {
				this.prePage = (this.pageNum - 1);
			}
			if (this.pageNum < this.pages) {
				this.nextPage = (this.pageNum + 1);
			}
		}
	}

	private void judgePageBoudary() {
		this.isFirstPage = (this.pageNum == 1);
		this.isLastPage = ((this.pageNum == this.pages) || (this.pages == 0));
		this.hasPreviousPage = (this.pageNum > 1);
		this.hasNextPage = (this.pageNum < this.pages);
	}

	public int getPageNum() {
		return this.pageNum;
	}

	public void setPageNum(int pageNum) {
		this.pageNum = pageNum;
	}

	public int getPageSize() {
		return this.pageSize;
	}

	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}

	public int getSize() {
		return this.size;
	}

	public void setSize(int size) {
		this.size = size;
	}

	public int getStartRow() {
		return this.startRow;
	}

	public void setStartRow(int startRow) {
		this.startRow = startRow;
	}

	public int getEndRow() {
		return this.endRow;
	}

	public void setEndRow(int endRow) {
		this.endRow = endRow;
	}

	public int getPages() {
		return this.pages;
	}

	public void setPages(int pages) {
		this.pages = pages;
	}

	@JsonIgnore
	@Deprecated
	public int getFirstPage() {
		return this.navigateFirstPage;
	}

	@Deprecated
	public void setFirstPage(int firstPage) {
		this.navigateFirstPage = firstPage;
	}

	public int getPrePage() {
		return this.prePage;
	}

	public void setPrePage(int prePage) {
		this.prePage = prePage;
	}

	public int getNextPage() {
		return this.nextPage;
	}

	public void setNextPage(int nextPage) {
		this.nextPage = nextPage;
	}

	@JsonIgnore
	@Deprecated
	public int getLastPage() {
		return this.navigateLastPage;
	}

	@Deprecated
	public void setLastPage(int lastPage) {
		this.navigateLastPage = lastPage;
	}

	public boolean isIsFirstPage() {
		return this.isFirstPage;
	}

	public void setIsFirstPage(boolean isFirstPage) {
		this.isFirstPage = isFirstPage;
	}

	public boolean isIsLastPage() {
		return this.isLastPage;
	}

	public void setIsLastPage(boolean isLastPage) {
		this.isLastPage = isLastPage;
	}

	public boolean isHasPreviousPage() {
		return this.hasPreviousPage;
	}

	public void setHasPreviousPage(boolean hasPreviousPage) {
		this.hasPreviousPage = hasPreviousPage;
	}

	public boolean isHasNextPage() {
		return this.hasNextPage;
	}

	public void setHasNextPage(boolean hasNextPage) {
		this.hasNextPage = hasNextPage;
	}

	public int getNavigatePages() {
		return this.navigatePages;
	}

	public void setNavigatePages(int navigatePages) {
		this.navigatePages = navigatePages;
	}

	public int[] getNavigatepageNums() {
		return this.navigatepageNums;
	}

	public void setNavigatepageNums(int[] navigatepageNums) {
		this.navigatepageNums = navigatepageNums;
	}

	public int getNavigateFirstPage() {
		return this.navigateFirstPage;
	}

	public int getNavigateLastPage() {
		return this.navigateLastPage;
	}

	public void setNavigateFirstPage(int navigateFirstPage) {
		this.navigateFirstPage = navigateFirstPage;
	}

	public void setNavigateLastPage(int navigateLastPage) {
		this.navigateLastPage = navigateLastPage;
	}

	@Override
	public String toString() {
		StringBuilder sb = new StringBuilder("PageInfo{");
		sb.append("pageNum=").append(this.pageNum);
		sb.append(", pageSize=").append(this.pageSize);
		sb.append(", size=").append(this.size);
		sb.append(", startRow=").append(this.startRow);
		sb.append(", endRow=").append(this.endRow);
		sb.append(", total=").append(this.total);
		sb.append(", pages=").append(this.pages);
		sb.append(", list=").append(this.list);
		sb.append(", prePage=").append(this.prePage);
		sb.append(", nextPage=").append(this.nextPage);
		sb.append(", isFirstPage=").append(this.isFirstPage);
		sb.append(", isLastPage=").append(this.isLastPage);
		sb.append(", hasPreviousPage=").append(this.hasPreviousPage);
		sb.append(", hasNextPage=").append(this.hasNextPage);
		sb.append(", navigatePages=").append(this.navigatePages);
		sb.append(", navigateFirstPage=").append(this.navigateFirstPage);
		sb.append(", navigateLastPage=").append(this.navigateLastPage);
		sb.append(", navigatepageNums=");
		if (this.navigatepageNums == null) {
			sb.append("null");
		} else {
			sb.append('[');
			for (int i = 0; i < this.navigatepageNums.length; i++) {
				sb.append(i == 0 ? "" : ", ").append(this.navigatepageNums[i]);
			}
			sb.append(']');
		}
		sb.append('}');
		return sb.toString();
	}

	public String getMessage() {
		return this.message;
	}

	public void setMessage(String message) {
		this.message = message;
	}

	public String getStatus() {
		return this.status;
	}

	public void setStatus(String status) {
		this.status = status;
	}
}